<?php


namespace App\Api\Hmyl;
use PhalApi\Api;
use PhalApi\Exception\BadRequestException;

/**
 * 韩美医疗
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Index extends Api
{

    public function getRules()
    {
        return array(
            'getWorkList'       => array(),
            'getCheckList'      => array(),
        );
    }

    /**
     * getCheckList
     */

     public function getCheckList()
     {

        $clientId = '1836613531034755074';
        $secret = 'ee0230ce26ef98db026cf1732097959f';
        $url = 'https://meas.bjhanmi.com.cn/prod-api/external/getCheckList';
        // $url = 'https://meas.hanmiit.com.cn/prod-api/external/getCheckList';

        $timestamp = date("Y-m-d H:i:s", time());
        $str = $clientId.$secret.$timestamp;
        $sign = md5($str);

        // 先实例
        $curl = new \PhalApi\CUrl();
        $curl->setHeader(array('Content-Type' => 'application/json'));
        
        $data = array(
            'clientId' => $clientId,
            'timestamp' => $timestamp,
            'sign' => $sign,
            'itemNos' => []
        );

        $rs = $curl->post($url, json_encode($data) , 3000);
        $rs = json_decode($rs, true);

        $itemNos = [];
        foreach ($rs['data'] as $element) {
            $itemNos[] = $element['itemNo'];
        }

        if($rs['code'] != 0){
            throw new BadRequestException($rs['msg'], 1);
        }

        if( !$rs['data']  ){
            return [$rs, $url];
        }

        $save = [];
        foreach($rs['data'] as $k => $v){

            $item = \PhalApi\DI()->notorm->zhjlgetworklist->where(  array( "taskId" => $v['taskId'], "itemNo" => $v['itemNo'] ) )->fetchOne();
            if (!$item ){
                $v['create_time'] = date("Y-m-d H:i:s", time());
                $v['sync'] = 0;
                $v['todo'] = 0;
                $v['backResult'] = 0;
                $v['backzs'] = 0;
                $save[]=\PhalApi\DI()->notorm->zhjlgetworklist->insert($v);
            }else{
                \PhalApi\DI()->notorm->zhjlgetworklist->where('id', $item['id'])->update(
                    array( "isNeedNoPass" => $v['isNeedNoPass'] ),
                );
            }
        }
        // 如果$save不为空，则发送通知
        if (!empty($save)) {
            // 实例化CUrl对象
            $curl = new \PhalApi\CUrl();
            // 设置请求URL
            $url = "https://api.wenfree.cn/?s=App.Notice.Dingding&content=【韩美有新任务】";
            // 发送GET请求，超时时间设置为3000毫秒
            $curl->get($url, 3000);

            $curl->get('https://api.wenfree.cn/?s=App.Hmyl_ItemNo.item', 60*1000);
            $curl->get('https://api.wenfree.cn/?s=App.Hmyl_Xiadan.Yuyue', 60*1000);
        }
        return $save;
     }


    /**
     * getWorkList
     */

    public function getWorkList()
    {
        $clientId = '1836613531034755074';
        $secret = 'ee0230ce26ef98db026cf1732097959f';
        $url = 'https://meas.bjhanmi.com.cn/prod-api/external/getWorkList';
        // $url = 'https://meas.hanmiit.com.cn/prod-api/external/getWorkList';

        $timestamp = date("Y-m-d H:i:s", time());
        $str = $clientId.$secret.$timestamp;
        $sign = md5($str);

        // 先实例
        $curl = new \PhalApi\CUrl();
        $curl->setHeader(array('Content-Type' => 'application/json'));
        
        $data = array(
            'clientId' => $clientId,
            'timestamp' => $timestamp,
            'sign' => $sign,
        );

        $rs = $curl->post($url, json_encode($data) , 3000);
        $rs = json_decode($rs, true);

        if($rs['code'] != 0){
            throw new BadRequestException($rs['msg'], 1);
        }

        $save = [];
        foreach($rs['data'] as $k => $v){

            $item = \PhalApi\DI()->notorm->zhjlgetworklist->where(  array( "taskId" => $v['taskId'], "itemNo" => $v['itemNo'] ) )->fetchOne();
            if (! $item ){
                $v['create_time'] = date("Y-m-d H:i:s", time());
                $v['sync'] = 0;
                $v['todo'] = 0;
                $save[]=\PhalApi\DI()->notorm->zhjlgetworklist->insert($v);
            }
        }
        return $save;
    }


    
}
